package pers.qianyu.month_202012.date_20201224;

import java.util.*;

/**
 * 1608. 特殊数组的特征值
 * https://leetcode-cn.com/problems/special-array-with-x-elements-greater-than-or-equal-x/
 *
 * @author mizzle rain
 * @date 2020-12-24 15:43
 */
public class SpecialArray {
    /**
     * - res <= len
     */
    public int specialArray(int[] nums) {
        int len = nums.length;
        Arrays.sort(nums);
        for (int i = 1; i <= len; i++) {
            if (isValid(i, nums)) {
                return i;
            }
        }
        return -1;
    }

    private boolean isValid(int target, int[] nums) {
        int count = 0;
        for (int num : nums) {
            if (num >= target) {
                count++;
            }
        }
        return count == target;
    }
}
